import { TreeNode } from "@/基本数据结构/二叉树";
export function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
    if (preorder.length !== inorder.length || !preorder.length) return null;
    const preTemp = JSON.parse(JSON.stringify(preorder))
    const inTemp = JSON.parse(JSON.stringify(inorder))
    if (preTemp.sort().join() !== inTemp.sort().join()) return null
    const root = new TreeNode(preorder[0])
    const index = inorder.findIndex((val) => val === preorder[0])
    root.left = buildTree(preorder.slice(1, index + 1), inorder.slice(0, index))
    root.right = buildTree(preorder.slice(index + 1, preorder.length), inorder.slice(index + 1, inorder.length))
    return root
};

   //1 2 4 5 3 6 7
   //4 2 5 1 6 3 7 